﻿using System;
using GeoAPI.Geometries;

namespace NetTopologySuite.Algorithm
{
	public class RobustDeterminant
	{
		public static int SignOfDet2x2(double x1, double y1, double x2, double y2)
		{
			long num = 0L;
			int num2 = 1;
			if (x1 == 0.0 || y2 == 0.0)
			{
				if (y1 == 0.0 || x2 == 0.0)
				{
					return 0;
				}
				if (y1 > 0.0)
				{
					if (x2 > 0.0)
					{
						return -num2;
					}
					return num2;
				}
				else
				{
					if (x2 > 0.0)
					{
						return num2;
					}
					return -num2;
				}
			}
			else
			{
				if (y1 != 0.0 && x2 != 0.0)
				{
					if (0.0 < y1)
					{
						if (0.0 < y2)
						{
							if (y1 > y2)
							{
								num2 = -num2;
								double num3 = x1;
								x1 = x2;
								x2 = num3;
								double num4 = y1;
								y1 = y2;
								y2 = num4;
							}
						}
						else if (y1 <= -y2)
						{
							num2 = -num2;
							x2 = -x2;
							y2 = -y2;
						}
						else
						{
							double num5 = x1;
							x1 = -x2;
							x2 = num5;
							double num6 = y1;
							y1 = -y2;
							y2 = num6;
						}
					}
					else if (0.0 < y2)
					{
						if (-y1 <= y2)
						{
							num2 = -num2;
							x1 = -x1;
							y1 = -y1;
						}
						else
						{
							double num7 = -x1;
							x1 = x2;
							x2 = num7;
							double num8 = -y1;
							y1 = y2;
							y2 = num8;
						}
					}
					else if (y1 >= y2)
					{
						x1 = -x1;
						y1 = -y1;
						x2 = -x2;
						y2 = -y2;
					}
					else
					{
						num2 = -num2;
						double num9 = -x1;
						x1 = -x2;
						x2 = num9;
						double num10 = -y1;
						y1 = -y2;
						y2 = num10;
					}
					if (0.0 < x1)
					{
						if (0.0 >= x2)
						{
							return num2;
						}
						if (x1 > x2)
						{
							return num2;
						}
					}
					else
					{
						if (0.0 < x2)
						{
							return -num2;
						}
						if (x1 < x2)
						{
							return -num2;
						}
						num2 = -num2;
						x1 = -x1;
						x2 = -x2;
					}
					for (;;)
					{
						num += 1L;
						double num11 = Math.Floor(x2 / x1);
						x2 -= num11 * x1;
						y2 -= num11 * y1;
						if (y2 < 0.0)
						{
							break;
						}
						if (y2 > y1)
						{
							return num2;
						}
						if (x1 > x2 + x2)
						{
							if (y1 < y2 + y2)
							{
								return num2;
							}
						}
						else
						{
							if (y1 > y2 + y2)
							{
								goto Block_26;
							}
							x2 = x1 - x2;
							y2 = y1 - y2;
							num2 = -num2;
						}
						if (y2 == 0.0)
						{
							goto Block_27;
						}
						if (x2 == 0.0)
						{
							return num2;
						}
						num11 = Math.Floor(x1 / x2);
						x1 -= num11 * x2;
						y1 -= num11 * y2;
						if (y1 < 0.0)
						{
							return num2;
						}
						if (y1 > y2)
						{
							goto Block_31;
						}
						if (x2 > x1 + x1)
						{
							if (y2 < y1 + y1)
							{
								goto Block_33;
							}
						}
						else
						{
							if (y2 > y1 + y1)
							{
								return num2;
							}
							x1 = x2 - x1;
							y1 = y2 - y1;
							num2 = -num2;
						}
						if (y1 == 0.0)
						{
							goto Block_35;
						}
						if (x1 == 0.0)
						{
							goto Block_37;
						}
					}
					return -num2;
					Block_26:
					return -num2;
					Block_27:
					if (x2 == 0.0)
					{
						return 0;
					}
					return -num2;
					Block_31:
					return -num2;
					Block_33:
					return -num2;
					Block_35:
					if (x1 == 0.0)
					{
						return 0;
					}
					return num2;
					Block_37:
					return -num2;
				}
				if (y2 > 0.0)
				{
					if (x1 > 0.0)
					{
						return num2;
					}
					return -num2;
				}
				else
				{
					if (x1 > 0.0)
					{
						return -num2;
					}
					return num2;
				}
			}
		}

		public static int OrientationIndex(Coordinate p1, Coordinate p2, Coordinate q)
		{
			double x = p2.X - p1.X;
			double y = p2.Y - p1.Y;
			double x2 = q.X - p2.X;
			double y2 = q.Y - p2.Y;
			return RobustDeterminant.SignOfDet2x2(x, y, x2, y2);
		}
	}
}
